package com.samoilovich.belarustoday.basefragments;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.mainscreen.MainActivity;
import com.samoilovich.belarustoday.tools.Utility;

public class OptionsMenuBaseFragment extends BaseFragment {
    private String mBackTitle = "";
    private View mUpdateLinerLayout;
    private Button mUpdateButton;
    private TextView mUpdateTextView;
    private View mPictureLoadingLinerLayout;
    private ToggleButton mPictureLoadingButton;
    private TextView mPictureLoadingTextView;
    private View mWriteReviewLinerLayout;
    private Button mWriteReviewButton;
    private TextView mWriteReviewTextView;
    private boolean mWasClickedBackButton;

    private boolean mUpdateButtonState;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        mRootView = inflater.inflate(R.layout.options_menu, null);
        mHideOptionsMenu = true;
        mWasClickedBackButton = false;
        if (mBackTitle.equals("")) {
            mBackTitle = getString(R.string.back_label);
        }
        prepareActionBarViews(R.string.options_label, mBackTitle, true);
        prepareViews();

        return mRootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (!mWasClickedBackButton) {
            sendLoadingActionBroadcastReceiver();
        }
    }

    @Override
    protected void onReceiveInternetIntent(boolean hasInternetConnection, Intent intent) {
        mUpdateButtonState = hasInternetConnection;
        if (mUpdateButton == null || mWriteReviewButton == null) {
            prepareViews();
        }
        if (mUpdateButtonState) {
            int color;
            if (getActivity() instanceof MainActivity) {
                color = getResources().getColor(R.color.unable_text_color);
                mUpdateTextView.setTextColor(color);
                mUpdateLinerLayout.setEnabled(false);
                mUpdateButton.setEnabled(false);
            } else {
                color = getResources().getColor(R.color.dark_gray);
                mUpdateTextView.setTextColor(color);
                mUpdateLinerLayout.setEnabled(true);
                mUpdateButton.setEnabled(true);
            }
            color = getResources().getColor(R.color.dark_gray);
            mWriteReviewTextView.setTextColor(color);
            mWriteReviewLinerLayout.setEnabled(true);
            mWriteReviewButton.setEnabled(true);
        } else {
            int color = getResources().getColor(R.color.unable_text_color);
            mUpdateTextView.setTextColor(color);
            mUpdateLinerLayout.setEnabled(false);
            mUpdateButton.setEnabled(false);
            mWriteReviewTextView.setTextColor(color);
            mWriteReviewLinerLayout.setEnabled(false);
            mWriteReviewButton.setEnabled(false);
        }
    }

    public void setBackTitle(int title) {
        mBackTitle = getString(title);
    }

    public void setBackTitle(String title) {
        mBackTitle = title;
    }

    public void setUpdateButtonState(boolean enabledState) {
        mUpdateButtonState = enabledState;
    }

    // -----------------------------------------------------------------------------------
    private void prepareViews() {
        onUpdateClick();
        onPictureLoadingManageClick();
        onWriteReviewClick();
        onAboutClick();
    }

    private void onUpdateClick() {
        mUpdateLinerLayout = mRootView.findViewById(R.id.update);
        mUpdateButton = (Button) mRootView.findViewById(R.id.button_update);
        mUpdateTextView = (TextView) mRootView.findViewById(R.id.textView_updateLabel);
        if (mUpdateButtonState) {
            mUpdateTextView.setTextColor(getResources().getColor(R.color.dark_gray));
            mUpdateLinerLayout.setEnabled(true);
            mUpdateButton.setEnabled(true);
        } else {
            mUpdateTextView.setTextColor(getResources().getColor(R.color.unable_text_color));
            mUpdateLinerLayout.setEnabled(false);
            mUpdateButton.setEnabled(false);
        }
        mUpdateLinerLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendLoadingActionBroadcastReceiver();
                mWasClickedBackButton = true;
                ((BaseFragmentActivity) getActivity()).update();
                RelativeLayout backView = (RelativeLayout) mRootView.findViewById(R.id.relativeLayout_back);
                backView.callOnClick();
            }
        });
    }

    private void onPictureLoadingManageClick() {
        mPictureLoadingLinerLayout = mRootView.findViewById(R.id.pictureLoading);
        mPictureLoadingTextView = (TextView) mRootView.findViewById(R.id.textView_managePictureLabel);
        mPictureLoadingButton = (ToggleButton) mRootView.findViewById(R.id.button_managePicture);
        boolean state = Utility.getPictureLoadingState(getActivity());
        mPictureLoadingButton.setChecked(state);
        updatePictureLoadingButtonState();
        mPictureLoadingLinerLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPictureLoadingButton.performClick();
                updatePictureLoadingButtonState();
            }
        });
    }

    private void onWriteReviewClick() {
        mWriteReviewLinerLayout = mRootView.findViewById(R.id.writeReview);
        mWriteReviewTextView = (TextView) mRootView.findViewById(R.id.textView_writeReview);
        mWriteReviewButton = (Button) mRootView.findViewById(R.id.button_writeReview);

        mWriteReviewLinerLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.samoilovich.belarustoday"));
                startActivity(browserIntent);
            }
        });
    }


    private void onAboutClick() {
        View info = mRootView.findViewById(R.id.about);
        info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((BaseFragmentActivity) getActivity()).setInfoFragment();
            }
        });
    }

    // -----------------------------------------------------------------------------------
    // Set ToggleButton state which dispatch picture loading flag and text for label.
    private void updatePictureLoadingButtonState() {
        if (mPictureLoadingButton.isChecked()) {
            mPictureLoadingTextView.setText(R.string.on_loading_pictures);
        } else {
            mPictureLoadingTextView.setText(R.string.off_loading_pictures);
        }
    }

    // Sending broadcast receiver which inform about state of "Loading picture" button
    private void sendLoadingActionBroadcastReceiver() {
        Utility.setPictureLoadingState(getActivity(), mPictureLoadingButton.isChecked());
        Intent intent = new Intent();
        intent.setAction(Constants.PICTURE_LOADING_ACTION);
        getActivity().sendBroadcast(intent);
    }

    // -----------------------------------------------------------------------------------
    private void updateUpdateAndLoadingPictureStates() {

    }

}
